package com.lixw.langchain.controller;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.TokenUsage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: LowApiController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/22 20:13
 **/
@Slf4j
@RestController
public class LowApiController {
    @Resource
    private ChatModel qwenChatModel;

    @Resource
    private ChatModel deepseekChatModel;

    @GetMapping("/lowapi/api01")
    public String api01(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
        String chat = qwenChatModel.chat(prompt);
        log.info("千问大模型返回数据{}",chat);
        return chat;
    }

    @GetMapping("/lowapi/api02")
    public String api02(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt) {
        ChatResponse chatResponse = deepseekChatModel.chat(UserMessage.from(prompt));
        String result = chatResponse.aiMessage().text();
        log.warn("通过langchain4j调用模型返回结果：{}",result);

        TokenUsage tokenUsage = chatResponse.tokenUsage();
        log.warn("本次调用消耗Token：{}",tokenUsage);

        return result;
    }
}